package com.ruoyi.zsyh.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.zsyh.domain.ZsyhFactoryPay;
import com.ruoyi.zsyh.service.IZsyhFactoryPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPartsTag;
import com.ruoyi.zsyh.service.IZsyhPartsTagService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.github.pagehelper.page.PageMethod.clearPage;

/**
 * 产品标签记录Controller
 * 
 * @author yuexl
 * @date 2021-12-24
 */
@Controller
@RequestMapping("/zsyh/partsTag")
public class ZsyhPartsTagController extends BaseController
{
    private String prefix = "zsyh/partsTag";

    @Autowired
    private IZsyhPartsTagService zsyhPartsTagService;
    @Autowired
    private IZsyhFactoryPayService zsyhFactoryPayService;

    @RequiresPermissions("zsyh:partsTag:view")
    @GetMapping()
    public String partsTag()
    {
        return prefix + "/partsTag";
    }

    /**
     * 查询产品标签记录列表
     */
    @RequiresPermissions("zsyh:partsTag:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhPartsTag zsyhPartsTag)
    {
        System.out.println("");
        startPage();
        List<ZsyhPartsTag> list = zsyhPartsTagService.selectZsyhPartsTagList(zsyhPartsTag);
        return getDataTable(list);
    }

    /**
     * 查询产品标签记录列表
     */
    @RequiresPermissions("zsyh:partsTag:list")
    @PostMapping("/listView")
    @ResponseBody
    public TableDataInfo listView(ZsyhPartsTag zsyhPartsTag)
    {
        startPage();
        List<ZsyhPartsTag> list = zsyhPartsTagService.selectZsyhPartsTagList(zsyhPartsTag);
        return getDataTable(list);
    }

    /**
     * 导出产品标签记录列表
     */
    @RequiresPermissions("zsyh:partsTag:export")
    @Log(title = "产品标签记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPartsTag zsyhPartsTag)
    {
        List<ZsyhPartsTag> list = zsyhPartsTagService.selectZsyhPartsTagList(zsyhPartsTag);
        ExcelUtil<ZsyhPartsTag> util = new ExcelUtil<ZsyhPartsTag>(ZsyhPartsTag.class);
        return util.exportExcel(list, "产品标签记录数据");
    }

    /**
     * 新增产品标签记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品标签记录
     */
    @Transactional
    @RequiresPermissions("zsyh:partsTag:add")
    @Log(title = "产品标签记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPartsTag zsyhPartsTag)
    {
        //设置操作人为当前登录人
        zsyhPartsTag.setTagSendName(ShiroUtils.getSysUser().getUserName());
        int num = zsyhPartsTagService.insertZsyhPartsTag(zsyhPartsTag);
        //生成服务费记录
        if(num >0){
            //获取当年年月
            ZsyhFactoryPay zsyhFactoryPay = new ZsyhFactoryPay();
            String dateString = DateUtils.getDate();
            Integer year = Integer.valueOf(dateString.substring(0,4));
            Integer month = Integer.valueOf(dateString.substring(5,7));
            zsyhFactoryPay.setPeriodYear(year);
            zsyhFactoryPay.setPeriodMonth(month);
            zsyhFactoryPay.setFactoryId(zsyhPartsTag.getFactoryId());
            List<ZsyhFactoryPay> zsyhFactoryPayList = zsyhFactoryPayService.selectZsyhFactoryPayList(zsyhFactoryPay);
            //存在记录只用更新即可
            if(zsyhFactoryPayList.size()>0){
                ZsyhFactoryPay zsyhFactoryPay1 = zsyhFactoryPayList.get(0);
                zsyhFactoryPay1.setPayOn(zsyhFactoryPay1.getPayOn().add(zsyhPartsTag.getTagMoney()));
                zsyhFactoryPay1.setPayEnd(zsyhFactoryPay1.getPayEnd().add(zsyhPartsTag.getTagMoney()));
                zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay1);
            }else{
                zsyhFactoryPay.setPayBegin(new BigDecimal(0));
                zsyhFactoryPay.setPayOn(zsyhPartsTag.getTagMoney());
                zsyhFactoryPay.setPayOff(new BigDecimal(0));
                zsyhFactoryPay.setPayEnd(zsyhPartsTag.getTagMoney());
                zsyhFactoryPayService.insertZsyhFactoryPay(zsyhFactoryPay);
            }
        }
        return toAjax(num);
    }

    /**
     * 修改产品标签记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPartsTag zsyhPartsTag = zsyhPartsTagService.selectZsyhPartsTagById(id);
        mmap.put("zsyhPartsTag", zsyhPartsTag);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品标签记录
     */
    @Transactional
    @RequiresPermissions("zsyh:partsTag:edit")
    @Log(title = "产品标签记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPartsTag zsyhPartsTag)
    {
        ZsyhPartsTag oldZsyhPartsTag = zsyhPartsTagService.selectZsyhPartsTagById(zsyhPartsTag.getId());
        int num = zsyhPartsTagService.updateZsyhPartsTag(zsyhPartsTag);
        if(num >0){
            //获取当年年月
            ZsyhFactoryPay zsyhFactoryPay = new ZsyhFactoryPay();
            String dateString = DateUtils.getDate();
            Integer year = Integer.valueOf(dateString.substring(0,4));
            Integer month = Integer.valueOf(dateString.substring(5,7));
            zsyhFactoryPay.setPeriodYear(year);
            zsyhFactoryPay.setPeriodMonth(month);
            zsyhFactoryPay.setFactoryId(zsyhPartsTag.getFactoryId());
            List<ZsyhFactoryPay> zsyhFactoryPayList = zsyhFactoryPayService.selectZsyhFactoryPayList(zsyhFactoryPay);
            //存在记录只用更新即可
            if(zsyhFactoryPayList.size()>0){
                ZsyhFactoryPay zsyhFactoryPay1 = zsyhFactoryPayList.get(0);
                zsyhFactoryPay1.setPayOn(zsyhFactoryPay1.getPayOn().add((zsyhPartsTag.getTagMoney().subtract(oldZsyhPartsTag.getTagMoney()))));
                zsyhFactoryPay1.setPayEnd(zsyhFactoryPay1.getPayEnd().add((zsyhPartsTag.getTagMoney().subtract(oldZsyhPartsTag.getTagMoney()))));
                zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay1);
            }else{
                return error("修改失败");
            }
        }
        return toAjax(num);
    }

    /**
     * 删除产品标签记录
     */
    @Transactional
    @RequiresPermissions("zsyh:partsTag:remove")
    @Log(title = "产品标签记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        ZsyhPartsTag zsyhPartsTag = zsyhPartsTagService.selectZsyhPartsTagById(ids);
        int num = zsyhPartsTagService.deleteZsyhPartsTagByIds(ids);
        //生成服务费记录
        if(num >0){
            //获取当年年月
            ZsyhFactoryPay zsyhFactoryPay = new ZsyhFactoryPay();
            String dateString = DateUtils.getDate();
            Integer year = Integer.valueOf(dateString.substring(0,4));
            Integer month = Integer.valueOf(dateString.substring(5,7));
            zsyhFactoryPay.setPeriodYear(year);
            zsyhFactoryPay.setPeriodMonth(month);
            zsyhFactoryPay.setFactoryId(zsyhPartsTag.getFactoryId());
            List<ZsyhFactoryPay> zsyhFactoryPayList = zsyhFactoryPayService.selectZsyhFactoryPayList(zsyhFactoryPay);
            //存在记录只用更新即可
            if(zsyhFactoryPayList.size()>0){
                ZsyhFactoryPay zsyhFactoryPay1 = zsyhFactoryPayList.get(0);
                zsyhFactoryPay1.setPayOn(zsyhFactoryPay1.getPayOn().subtract(zsyhPartsTag.getTagMoney()));
                zsyhFactoryPay1.setPayEnd(zsyhFactoryPay1.getPayEnd().subtract(zsyhPartsTag.getTagMoney()));
                zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay1);
            }else{
                return error("删除出错");
            }
        }
        return toAjax(num);
    }
}
